var app = getApp()
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    errors: 0,
    exercise: null,
    course: null,
    loading: true
  },
  goLogin: function(){
    wx.reLaunch({
      url: '../index/welcome',
    })
  },
  gocourse: function(){
    wx.navigateToMiniProgram({
      appId: 'wx2e984de3a40d7b4d',
      path: 'pages/course/index',
    })
  },
  goexperience: function(){
    wx.navigateToMiniProgram({
      appId: 'wx2e984de3a40d7b4d',
      path: 'pages/course/description',
    })
  },
  onLoad(){
    var that = this
    //查询最新课程
    wx.request({
      url: 'https://www.mangguoxinli.com/api/course/getLatestCourse',
      method: 'POST',
      success:function(res){
        that.setData({
          course: res.data.course
        })
      }
    })
  },

  onShow(){
    var that = this
    var openid = app.globalData.openid
    if(!openid){
      openid = wx.getStorageSync('openid')
    }
    if(!openid){
      // 调用云函数
      wx.cloud.callFunction({
        name: 'login',
        data: {},
        success: res => {
          app.globalData.openid = res.result.openid
        },
        fail: err => {
          console.error( err)
        }
      })
    }
    if(openid){
      db.collection('userInfo').where({
        _openid: openid
      }).get({
        success: res=>{
          that.setData({
            userInfo: res.data[0]
          })

          // const add_time = res.data[0].add_time.getTime()
          // const now_time = (new Date()).getTime()
          // const limit = parseInt((now_time - add_time)/1000/86400) + 1
          const _ = db.command
          //.limit(limit)
          db.collection('exercise').orderBy('add_time','desc').get({
            success: res => {
              var exercise =  res.data
              exercise.forEach(function(item,index){
                db.collection('userTest').where({
                  _openid: openid,
                  exercise_id: item._id
                }).get({
                  success: res => {
                    if(res.data.length > 0){
                      item.user_tested = true
                    }else{
                      item.user_tested = false
                    }
                    console.log('userTest',res.data)
                  }
                })
              })
              setTimeout(function(){
                that.setData({
                  exercise,
                  loading: false
                })
              },1000)
             
              
            }
          })
          
        }, 
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '查询记录失败'
          })
        }
        
      })
      //查询错题数
      db.collection('userWrong').where({
        _openid: openid
      }).count({
        success: res => {
          that.setData({
            errors: res.total
          })
        }
      })
    }    
  },
  onShareAppMessage: function () {
    return {
      title: '微笑题酷',
      path: 'pages/index/index'
    }
  },
  /**
  * 分享到朋友圈
  */
 onShareTimeline: function(){
   return {
     title: '微笑题酷',
     imageUrl: '../../images/logo.png'
   }
 }
})